package xyz.xtt.asset.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;

import cn.hutool.core.util.StrUtil;
import xyz.xtt.asset.api.enums.AssetTypeEnum;
import xyz.xtt.asset.constants.AssetCache;
import xyz.xtt.asset.entity.TblCollection;
import xyz.xtt.asset.mapper.TblCollectionMapper;
import xyz.xtt.asset.service.ITblAssetsTypeService;
import xyz.xtt.asset.service.ITblCollectionService;
import xyz.xtt.asset.vo.AssetTypeResponseVo;
import xyz.xtt.asset.vo.CollectionVo;
import xyz.xtt.asset.vo.HomeTopResponse;
import xyz.xtt.asset.vo.QueryCollType;
import xyz.xtt.asset.vo.QueryCollectionResponseVo;
import xyz.xtt.common.starter.cache.CacheManager;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-02-13
 */
@Service
public class TblCollectionServiceImpl extends ServiceImpl<TblCollectionMapper, TblCollection> implements ITblCollectionService {
	private static final String NFT = "nft";
	@Autowired
	private ITblAssetsTypeService iTblAssetsTypeService;

	@Autowired
	private CacheManager cacheManager;

	/**
	 * 更新风格使用次数
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public boolean plusVer(Long id) {
		LambdaUpdateChainWrapper<TblCollection> update = new LambdaUpdateChainWrapper<>(baseMapper);
		return update.eq(TblCollection::getId, id).setIncrBy(TblCollection::getVer, 1).update();
	}

	@Override
	public QueryCollType createQuery(String uid) {
		List<QueryCollectionResponseVo> type = cacheManager.cacheList(() -> type(uid), AssetCache.COLL_QUERY_TYPE, uid);
		List<AssetTypeResponseVo> search = cacheManager.cacheList(() -> iTblAssetsTypeService.search(NFT), AssetCache.ASSET_TYPE).stream().filter(one -> {
			return !StrUtil.equals(one.getCode(), AssetTypeEnum.INTEREST.groupType().toUpperCase());
		}).toList();
		QueryCollType build = QueryCollType.builder().coll(type).type(search).build();
		return build;
	}

	@Override
	public List<QueryCollectionResponseVo> type(String uid) {
		MPJLambdaWrapper<TblCollection> query = JoinWrappers
				.lambda(TblCollection.class)
				.eq(TblCollection::getAppUserId, uid)
				.select(TblCollection::getId, TblCollection::getDisplayName, TblCollection::getCoverImg)
				.orderByDesc(TblCollection::getLastModifyDatetime);
		return query.list(QueryCollectionResponseVo.class);
	}

	/**
	 * 产页置顶风格
	 *
	 * @return
	 */
	@Override
	public List<HomeTopResponse> homeTop() {
		return cacheManager.cacheList(() -> homeTopQuery(), AssetCache.HOME_TOP);
	}

	List<HomeTopResponse> homeTopQuery() {
		MPJLambdaWrapper<TblCollection> query = JoinWrappers
				.lambda(TblCollection.class)
				.ne(TblCollection::getTopTime, 0)
				.select(TblCollection::getId, TblCollection::getDisplayName, TblCollection::getCoverImg, TblCollection::getAppUserId)
				.orderByDesc(TblCollection::getTopTime);
		return query.list(HomeTopResponse.class);
	}

	/**
	 * 获取 系统风格列表
	 * 
	 * @return
	 */
	@Override
	public List<CollectionVo> getListAll() {
		List<TblCollection> tblCollections = new LambdaQueryChainWrapper<>(baseMapper)
				.eq(TblCollection::getAppUserId, 0)
				.orderByAsc(TblCollection::getId)
				.list();
		List<CollectionVo> list = new ArrayList<>();
		tblCollections.forEach(tblCollection -> {
			CollectionVo build = CollectionVo
					.builder()
					.id(tblCollection.getId())
					.name(tblCollection.getDisplayName())
					.description(tblCollection.getDescription())
					.imgUrl(tblCollection.getCoverImg())
					.count(tblCollection.getVer())
					.build();
			list.add(build);
		});
		return list;
	}
}
